package com.database.service.impl;


import com.database.character.Post;
import com.database.character.Praise;
import com.database.mapper.PostMapper;
import com.database.mapper.PraiseMapper;
import com.database.service.PraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PraiseServiceImpl implements PraiseService {

  @Autowired
  private PostMapper postMapper;

  @Autowired
  private PraiseMapper praiseMapper;


  @Override
  public int addPraise(Praise praise) {
    Integer postId = praise.getPostId();
    Post post = postMapper.getPostByPid(postId);
    post.setUpdateTime(LocalDateTime.now());
    post.setPraiseNum(post.getPraiseNum() + 1);
    postMapper.updatePost(post);//对应帖子的点赞数加一
    return praiseMapper.addPraise(praise);
  }

  @Override
  public int deletePraiseByPostId(Integer postId) {
    Post post = postMapper.getPostByPid(postId);
    post.setUpdateTime(LocalDateTime.now());
    post.setPraiseNum(post.getPraiseNum() - 1);
    postMapper.updatePost(post);//对应帖子的点赞数减一
    return praiseMapper.deletePraiseByPostId(postId);
  }

  @Override
  public List<Praise> getPraiseByUid(Integer userId) {
    return praiseMapper.getPraiseByUid(userId);
  }
}
